import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Widgets {
    private JFrame mainFrame;
    private JButton saveButton;
    private JButton loadButton;
    private JTextArea textFrame;
    private JMenuItem openItem;
    private JMenuItem saveItem;
    private JMenuItem exitItem;
    private JMenuItem helpItem;
    private JMenuItem aboutItem;
    private JMenuItem saveAsItem;
    
    Widgets() {
        // Main window
        mainFrame = new JFrame("Simple text editor");
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setSize(500, 300);
        mainFrame.setLocationRelativeTo(null); 
        mainFrame.setMinimumSize(new Dimension(500, 300));
        mainFrame.setMaximumSize(new Dimension(800, 500));  // doesn't work!!
        mainFrame.setPreferredSize(new Dimension(500, 300));
        mainFrame.setResizable(true);

        // Menu
        JMenuBar menuBar = new JMenuBar();
        
        JMenu fileMenu = new JMenu("File");
        fileMenu.setDisplayedMnemonicIndex(0);
        saveItem = new JMenuItem("Save");
        saveItem.setDisplayedMnemonicIndex(0);
        saveItem.setEnabled(false);
        saveAsItem = new JMenuItem("Save as");
        saveAsItem.setDisplayedMnemonicIndex(0);
        
        openItem = new JMenuItem("Open");
        openItem.setDisplayedMnemonicIndex(0);

        exitItem = new JMenuItem("Exit");
        exitItem.setDisplayedMnemonicIndex(0);

        JMenu helpMenu = new JMenu("Help");
        helpMenu.setDisplayedMnemonicIndex(0);
        helpItem = new JMenuItem("Help");
        helpItem.setEnabled(false);
        helpItem.setDisplayedMnemonicIndex(0);
        aboutItem = new JMenuItem("About");
        aboutItem.setDisplayedMnemonicIndex(0);
        aboutItem.setEnabled(false);

        fileMenu.add(openItem);
        fileMenu.add(saveItem);
        fileMenu.add(saveAsItem);
        fileMenu.addSeparator();
        fileMenu.add(exitItem);

        helpMenu.add(helpItem);
        helpMenu.addSeparator();
        helpMenu.add(aboutItem);

        menuBar.add(fileMenu);
        menuBar.add(helpMenu);
        mainFrame.setJMenuBar(menuBar);

        // Base panel
        JPanel basePanel = new JPanel();
        basePanel.setLayout(new BoxLayout(basePanel, BoxLayout.LINE_AXIS));

        // Text frame
        textFrame = new JTextArea();
        JScrollPane scrollPane = new JScrollPane(textFrame);
        scrollPane.setAlignmentY(Component.TOP_ALIGNMENT);
        basePanel.add(scrollPane);

        mainFrame.getContentPane().add(basePanel);
    }
    
    public String getText() {
        return textFrame.getText();
    }

    public void setOpenAction(ActionListener aL) {
        openItem.addActionListener(aL);
    }

    public void setSaveAction(ActionListener aL) {
        saveItem.addActionListener(aL);
    }

    public void setSaveAsAction(ActionListener aL) {
        saveAsItem.addActionListener(aL);
    }

    public void setExitAction(ActionListener aL) {
        exitItem.addActionListener(aL);
    }

    public void setHelpAction(ActionListener aL) {
        exitItem.addActionListener(aL);
    }

    public void setAboutAction(ActionListener aL) {
        exitItem.addActionListener(aL);
    }

    public void setText(String t) {
        textFrame.setText(t);
    }

    public String chooseFile(String type) {
        String st;
        JFileChooser fileopen = new JFileChooser();
        int ret = fileopen.showDialog(null, type);                
        if (ret == JFileChooser.APPROVE_OPTION) {
            st = fileopen.getSelectedFile().getAbsolutePath();
        } else {
            st = null;
        }
        return st;
    }

    public void showSuccessMessage(String message) {
        JOptionPane.showMessageDialog(this.mainFrame,message,"Confirmation",
                                      JOptionPane.INFORMATION_MESSAGE);
    }

    public void showErrorMessage(String message) {
        JOptionPane.showMessageDialog(this.mainFrame,message,"Error",
                                      JOptionPane.ERROR_MESSAGE);
    }

    public boolean getConfirmation(String message) {
        if (JOptionPane.showConfirmDialog(null,message) == JOptionPane.YES_OPTION) {
            return true;
        } else {
            return false;
        }
    }

    public void setSaveState(Boolean state) {
        this.saveItem.setEnabled(state);
    }

    public void mainFrameShow() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                mainFrame.setVisible(true);
            }
        });
    }
}